<?php

abstract class Maverick_Controller_Action extends Zend_Controller_Action{
	
	protected $_auth;

	public function __construct(Zend_Controller_Request_Abstract $request, Zend_Controller_Response_Abstract $response, array $invokeArgs = array()){
		parent::__construct($request, $response, $invokeArgs = array());
    	
		$this->view->addBasePath('../application/modules/default/views/');
	}
	public function init()
    {
    	$this->_auth = Zend_Auth::getInstance();
    	
    	$user = $this->_auth->getStorage()->read();
    	if ($user == 'admin'){
    		$sql = "select * from `modules";
    	} else {
    		$sql = 'SELECT * FROM `permissions` inner join modules on modules.id = permissions.modules_id inner join users on users.id = permissions.users_id where users.login = \''.$user.'\'';
    	}
    	$object = new Modules();
    	$modules = $object->getAdapter()->fetchAll($sql);
    	if ($modules){
    	}else {
    		//$modules =  array(0 => array(' ' => ' '));
    	}
    	$this->view->modules = $modules;
    }

    public function deniedAction(){
    	//$this->_forward('denied', 'user');
    }
 	
    protected function _prepereList($object, $pageNo = 1, $onPage = 2){
    	$pageNo = ($pageNo == 0)?1:$pageNo;
    	
    	$pocz = ($pageNo - 1) * $onPage;
    	$kon  = ($pageNo - 1) * $onPage + $onPage - 1;
    	$kon  = ($kon > count($object) - 1)?$kon : count($object);
    	
    	
    	$this->view->show = ($onPage >= count($object))?false:true;
    	
    	$this->view->pageNo = $pageNo;
    	$this->view->arrayItem = array_slice($object,$pocz,$onPage);
    	$pageCount = floor((int)count($object) / (int)$onPage);
    	$d = (int)count($object) % (int)($onPage);
    	if ($d > 0){
    		$pageCount++;
    	}
		//echo $pageCount;
    	$this->view->pageCount = $pageCount;		
    		
    	
    }
    public function setPathbar($array = null){
    	
    	$path 	= "";
    	$string = "";
    	if ($array){
	    	$c 	 = count($array);
	    	$ind = 0;
    		foreach ($array as $ar => $key){
    			$ind++;
    			if ($ind == $c){
    				$string = 'class="selected"';
    			}
    			$path .= '<li class="hr">></li>';
    			$path .= '<li><a '.$string.'href="'.$key.'">'.$ar.'</a></li>';
    		}
    	}
    	
    	$helper = $this->view->getHelper('Pathbar');
    	$helper->setPath($path);
    }
     protected function _redirect($url, array $options = array()){
    	header("Location: $url");
    }
	/**
	 * Funckcja dla zadanej sciezki do pliku (wzgledem pliku index.php) sprawdza czy dany plik istnieje,
	 * jesli tak to tworzy unikalna nazwę.
	 * FIXME może lepsze będzie przeneisienei do klasy statycznej zawierającej metody do działania na plikach
	 *
	 * @param string $path
	 * @return string
	 */
    protected function _getName($path){
    	
    	if (file_exists($path)){
	    	$name = substr($path, strpos($path,strstr($path,'/')));
	    	//$name = strpos($path,strstr($path,'/'));
	    	$name = str_replace('/','',$name);
	    	$exp = substr($name,strrpos($name,'.'));
	    	$file = substr($name,0,strrpos($name,'.'));
	    	$ok = true;
	    	$licznik = 1;
	    	while ($ok){
	    		$newName = $file.$licznik.$exp;
	    		if (file_exists(str_replace($name,$newName,$path))){
	    			$ok = true;
	    			$licznik++;
	    		} else {
	    			$ok = false;
	    		}
	    	}
	    	return str_replace($name,$newName,$path);
    	}
    	return $path;
    }
}
?>